package com.zjp.blog.controller.listener;

import com.zjp.blog.base.ApplicationContext;
import com.zjp.blog.data.model.Config;
import com.zjp.blog.service.ConfigService;
import com.zjp.blog.utils.Logging;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author pj_zhong
 * @email pj_zhong@163.com / pj_zhong@gmail.com
 */

@Component
public class StartupInitialization implements InitializingBean{

    @Autowired
    ConfigService configService;

    @Autowired
    ApplicationContext appContext;


    public void afterPropertiesSet() throws Exception {
        Logging.info("System initializing");

        List<Config> configs = configService.listAll();

        if(configs.isEmpty()){
            Logging.error("Config Properties load failed,maybe the database is not ready yet");
            System.exit(1);
        }else{
            appContext.setSystemConfigs(configs);
        }


        /*web site_url*/
        String basepath = appContext.getServletContext().getContextPath();

        /*static resouces path*/
        String staticpath = basepath  + "/resources";

        appContext.getServletContext().setAttribute("basepath",basepath);
        appContext.getServletContext().setAttribute("staticpath",staticpath);


        Logging.info("System initialization done");
    }


}
